package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrAttrgroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author yangshen
 * @email yangshen@gmail.com
 * @date 2020-05-25 17:51:12
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Resource
    private ProductAttrValueService productAttrValueService;

    /**
     * 获取指定商品下的基本属性
     * @param spuId 商品id
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpuId(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entities =productAttrValueService.baseAttrListForSpuId(spuId);
        return R.ok().put("data", entities);
    }

    /**
     * 基本属性(规则参数)和销售属性共用同一张表，通过attr_type进行区分
     * attrType = base, 基本属性
     * attrType = sale, 销售属性
     * @param params 入参
     * @param catelogId 分类主键id
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId")Long catelogId,
                                                                    @PathVariable("attrType")String attrType){
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     *
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息   修改 --返回属性及其相关信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     * 基本属性(规则参数)和销售属性共用同一张表，通过attr_type进行区分
     *      * attrType = base, 基本属性
     *      * attrType = sale, 销售属性
     *
     */
    @RequestMapping("/save")
//    public R save(@RequestBody AttrEntity attr){
//		attrService.save(attr);
    public R save(@RequestBody AttrVo attrVo){
        attrService.saveAttrVo(attrVo);
        return R.ok();
    }



    /**
     * 基本属性(规则参数)和销售属性共用同一张表，通过attr_type进行区分
     *      * attrType = base, 基本属性
     *      * attrType = sale, 销售属性
     * 修改
     */
    @RequestMapping("/update")
//    public R update(@RequestBody AttrEntity attr){
//		attrService.updateById(attr);
    public R update(@RequestBody AttrVo attrVo){
        attrService.updateAttr(attrVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
